﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace Devsoft.Web.Mvc
{
    public class AutoCommitController:Controller
    {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            Devsoft.DB.Models.Transaction.Current.Enter();
            base.OnActionExecuting(filterContext);
        }

        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            var transaction = Devsoft.DB.Models.Transaction.Current;
            base.OnActionExecuted(filterContext);
            if (filterContext.Exception != null)
            {
                transaction.Rollback();    
            }
            else
            {
                transaction.Commit();
            }
            var db = Devsoft.DB.Models.DBRouter.Default.CurrentDataContext;
            db.Dispose();
        }
    }
}
